package com.naire.modul.district.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.district.dao.DistrictDao;
import com.naire.modul.district.pojo.District;
import com.naire.modul.district.vo.DistrictVo;

public class DistrictDaoImpl extends GeneralDaoImpl implements DistrictDao {

	@SuppressWarnings("unchecked")
	public List<DistrictVo> searchRoot() throws Exception {
		StringBuilder hql = new StringBuilder("from District where flag=0 and parent_code is null");
		List<DistrictVo> d = new ArrayList<DistrictVo>();
		List<District> l = hibernateHandler.query(hql);
		for(District bean : l) {
			DistrictVo dv = new DistrictVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DistrictVo> search(PageBean pageBean, String code) throws Exception {
		StringBuilder hql = new StringBuilder("from District where flag=0");
		Map<String, Object> params = new HashMap<String, Object>();
		if(code != null && !code.equals("")) {
			params.put("parent_code", code);
			hql.append(" and parent_code=:parent_code");
		} else {
			hql.append(" and parent_code is null");
		}
		List<DistrictVo> d = new ArrayList<DistrictVo>();
		List<District> l = hibernateHandler.query(hql, pageBean, params);
		for(District bean : l) {
			DistrictVo dv = new DistrictVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistrictVo> searchByCode(String code) throws Exception {
		List<DistrictVo> d = new ArrayList<DistrictVo>();
		StringBuilder hql = new StringBuilder("from District where flag=0");
		List<District> l = new ArrayList<District>();
		if(code != null && !code.equals("")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("parent_code", code);
			hql.append(" and parent_code=:parent_code");
			l = hibernateHandler.query(hql, map);
		} else {
			hql.append(" and parent_code is null");
			l = hibernateHandler.query(hql);
		}
		
		for(District bean : l) {
			DistrictVo dv = new DistrictVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(DistrictVo bean) throws Exception {
		District pojo = new District();
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void insert(List<DistrictVo> list) throws Exception {
		int j = 0;
		for(DistrictVo vo : list) {
			if(j % 20 == 0) {
				hibernateHandler.flush();
			}
			insert(vo);
			j++;
		}
	}

	@Override
	public void update(DistrictVo bean) throws Exception {
		District pojo = (District)hibernateHandler.find(District.class, bean.getDistrict_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(DistrictVo bean) throws Exception {
		update(bean);
	}

	@Override
	public DistrictVo find(Integer id) throws Exception {
		District pojo = (District)hibernateHandler.find(District.class, id);
		DistrictVo bean = new DistrictVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}
}
